<template>
  <TableView class="table">
    <template #form>
      <NSpace justify="space-between">
        <NSpace align="center">
          标题：
          <NInput style="width: 200rem" placeholder="请输入标题" v-model:value="paginationReactive.title" clearable />
          时间范围：
          <NDatePicker input-readonly placeholder="请选择时间范围" style="width: 300rem" clearable v-model:value="range"
            type="daterange" />
          审批状态：
          <NSelect :options="statusList" placeholder="请选择审批状态" clearable style="width: 200rem"
            v-model:value="paginationReactive.status"  />
          <NButton type="primary" style="width: 78rem" ghost @click="searchTable">
            查询
          </NButton>
        </NSpace>
      </NSpace>
    </template>

    <template #table="props">
      <NDataTable v-bind="props" :bordered="false" :columns="columns" :data="tableData" :pagination="false"
        :loading="tableLoading" />
    </template>

    <template #pagination>
      <NPagination :page-sizes="paginationReactive.pageSizes" :item-count="paginationReactive.itemCount"
        :page="paginationReactive.page" :page-size="paginationReactive.pageSize"
        @update:page="paginationReactive.onChange" @update:page-size="paginationReactive.onUpdatePageSize"
        show-size-picker show-quick-jumper :suffix="() => '页'">
        <template #prefix>
          <span style="font-size: 14px;"> 共{{ paginationReactive.itemCount }}条</span>
        </template>
      </NPagination>
    </template>
  </TableView>
</template>
<script setup lang="ts">
import { h, reactive, watch, ref, onDeactivated } from 'vue'
import {
  NSpace,
  NInput,
  NButton,
  NDataTable,
  NPagination,
  NDatePicker,
  NSelect,
  useMessage
} from 'naive-ui'
import type { DataTableColumns } from 'naive-ui'
import TableView from '@/components/TableView.vue'
import router from '@/router'
import { sendRequest } from '@/utils/api'
import type { HttpResponse } from '@/utils/http'
import type { DisModel } from '@/api/receiveOrDispatch'
import { format } from 'date-fns'
import { useRoute } from 'vue-router'
import { useViewerStore } from '@/views/OA/Common/store'

const useStore = useViewerStore()
const message = useMessage()
const route = useRoute()
const range = ref()
const statusList = [
  { label: '撤销', value: 'A0602' },
  { label: '审批中', value: 'A0603' },
  { label: '拒绝', value: 'A0604' },
  { label: '通过', value: 'A0605' }
]

const columns: DataTableColumns = [
  {
    title: '用户',
    key: 'userName',
    width: 60,
    align: useStore.isCenter
  },
  {
    title: '部门',
    key: 'deptName',
    width: 100,
    align: useStore.isCenter
  },
  {
    title: '标题',
    key: 'description',
    width: 100,
    align: useStore.isCenter,
    ellipsis: {
      tooltip: {
        contentStyle: {
          maxWidth: '400rem'
        }
      }
    }
  },
  {
    title: '类型',
    key: 'typeName',
    width: 60,
    align: useStore.isCenter
  },
  {
    title: '说明',
    key: 'remark',
    width: 200,
    ellipsis: {
      tooltip: {
        contentStyle: {
          maxWidth: '400px'
        }
      }
    },
    align: useStore.isCenter
  },
  {
    title: '发布时间',
    key: 'updatedTime',
    width: 100,
    align: useStore.isCenter
  },
  {
    title: '审批状态',
    key: 'approvalStatus',
    width: 60,
    align: useStore.isCenter
  },
  {
    title: '操作',
    key: 'action',
    align: useStore.isCenter,
    width: 100,
    fixed: 'right',
    render: (row: any) => [
      h(
        NButton,
        {
          type: 'primary',
          quaternary: true,
          onClick: () => {
            router.push({ path: '/OA/ReceiveOrDispatch/DispatchInfo', query: { id: row.id } })
          }
        },
        {
          default: () => '详情'
        }
      ),
      h(
        NButton,
        {
          type: 'primary',
          quaternary: true,
          style: { display: row.approvalStatus == '通过' ? 'inlne-block' : 'none' },
          onClick: () => {
            router.push({ path: '/OA/ReceiveOrDispatch/DispatchSign', query: { id: row.id } })
          }
        },
        {
          default: () => '签收情况'
        }
      )
    ]
  }
]
// 上一页下一页
const paginationReactive = reactive<Record<string, any>>({
  page: 1,
  pageSize: 10,
  pageSizes: [10, 20, 50, 100, 500],
  itemCount: 50,
  title: null, //标题
  startTime: null, //开始时间
  endTime: null, //结束时间
  type: null, //类型
  onChange: (page: number) => {
    paginationReactive.page = page
    getStaticticsList()
  },
  onUpdatePageSize: (pageSize: number) => {
    paginationReactive.pageSize = pageSize
    paginationReactive.page = 1
    getStaticticsList()
  }
})
// 获取列表
const tableData = ref([])
const tableLoading = ref(false)
const getStaticticsList = async () => {
  tableLoading.value = true
  const [error, res] = await sendRequest<HttpResponse<DisModel>>({
    module: 'receiveOrDispatch',
    api: 'dispatchList',
    override: {
      params: {
        page: paginationReactive.page,
        limit: paginationReactive.pageSize,
        title: paginationReactive.title,
        startTime: paginationReactive.startTime,
        endTime: paginationReactive.endTime,
        status: paginationReactive.status,
        isAll: true
      }
    }
  })
  tableLoading.value = false
  if (error) return message.error(error.message)
  if (res.code !== 0) return message.error(res.msg)
  tableData.value = []
  setTimeout(() => {
    tableData.value = res.data.list
  }, 0)
  paginationReactive.itemCount = res.data.totalCount
}
// 查询
const searchTable = () => {
  paginationReactive.page = 1
  if (range.value) {
    paginationReactive.startTime = format(range.value[0], 'yyyy-MM-dd')
    paginationReactive.endTime = format(range.value[1], 'yyyy-MM-dd')
  } else {
    paginationReactive.startTime = null
    paginationReactive.endTime = null
  }
  getStaticticsList()
}

const keyUpDown = (e: any) => e.key == 'Enter' && (paginationReactive.page = 1,getStaticticsList())
onDeactivated(() => window.removeEventListener('keyup', keyUpDown, false))

watch(
  () => route,
  async () => {
    if (route.path == '/OA/ReceiveOrDispatch/dispatchStatictics') {
      getStaticticsList()
      window.addEventListener('keyup', keyUpDown, false) // 回车
    }
  },
  { deep: true, immediate: true }
)
</script>
<style scoped lang="scss">
.table {
  padding: 29rem 30rem 42rem;
}
</style>
